package com.etalk.crm.controller.newmain;

import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.NewMainBean;
import com.etalk.crm.service.CCNewMainService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

import static com.etalk.crm.utils.PubResult.pubResultFunction;

/**
 * @Auther: James
 * @Date: 2018/12/28 09:48
 * @Description: 课程顾问首页工作台
 */
@Controller
@RequestMapping(value = "/CCNewMain",produces = "application/json; charset=utf-8")
public class CCNewMainManageCtl {
    protected static final Logger logger = LogManager.getLogger(CCNewMainManageCtl.class);

    @Resource
    private CCNewMainService ccNewMainService;

    /**
     *首页cc 预约与回访(提醒)
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectTrackRemindByccId",method = RequestMethod.POST)
    @ResponseBody
    public String selectTrackRemindByccId(Integer ccId,Integer page, Integer limit, String searchDate, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectTrackRemindByUserId(ccId,searchDate,null,null,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc提醒列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *根据cc ssc 查询关联学生订单的60天到期信息或课时剩30以下
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/searchStuOrderInfoByCC",method = RequestMethod.POST)
    @ResponseBody
    public String searchStuOrderInfoByCC(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchStuOrderInfoByCC(ccId,null,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc学生订单的60天到期信息或课时剩30以下列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *课程顾问demo课已约未上学员
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/demoMissedListByCC",method = RequestMethod.POST)
    @ResponseBody
    public String demoMissedListByCC(Integer ccId,Integer page, Integer limit, String searchDate, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.demoMissedListByCC(ccId,searchDate,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc demo课已约未上学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 查询首页课程顾问下潜在学员
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipPotentialByCCId",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipPotentialByCCId(Integer ccId,Integer state, Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipPotentialByCCId(ccId,state,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc下潜在学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }


        /**
     * 查询首页课程顾问下待约学员
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/appointedStudentByccId",method = RequestMethod.POST)
    @ResponseBody
    public String appointedStudentByccId(Integer ccId,Integer state, Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        if(state == null){
            //已上可跟进
            state = 6;
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipPotentialByCCId(ccId,state,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc下待约学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }


    /**
     *查询首页渠道为RL(推荐)的所有学员(Ref Leads)
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipRelLeadsByCC",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipRelLeadsByCC(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipRelLeadsByCC(ccId,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc 渠道为RL(推荐)学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *查询首页新学员(第一次购买套餐在一个月31天内)
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipNewStudentByCCId",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipNewStudentByCCId(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipNewStudentByCCId(ccId,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc 新学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *oc课课后回访
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectOcInfoByCC",method = RequestMethod.POST)
    @ResponseBody
    public String selectOcInfoByCC(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectOcInfoByCC(ccId,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc oc课课后回访列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    @RequestMapping(value = "/selectTeacherRank", method = RequestMethod.POST)
    @ResponseBody
    public String selectTeacherRank(Integer ccId, String searchKey, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectTeacherRank(ccId, searchKey, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页老师投诉学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 学生旷课或者取消上课记录(首页未正常完成课程学员面板)
     *
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/abnormalunFinishLessons", method = RequestMethod.POST)
    @ResponseBody
    public String selectLessonState(Integer ccId, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectLessonState(ccId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页未正常完成课程学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 待约家长会提醒
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录
     * @param session http
     * @return
     */
    @RequestMapping(value = "/booking/lct/class", method = RequestMethod.POST)
    @ResponseBody
    public String getBookingLctClass(Integer ccId, Integer finishId,Integer yearMonth,Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchBookingLctClassByCcId(ccId, finishId,yearMonth,page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页学管师学员上完中教课提醒家长会课程列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 未设置动态提醒学员列表
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录
     * @param session http
     * @return
     */
    @RequestMapping(value = "/no/reminder/time/list", method = RequestMethod.POST)
    @ResponseBody
    public String getNoReminderTimeStudentList(Integer ccId, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchNoReminderTimeStudentList(ccId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页未设置动态提醒学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 订单总数小于40课时/币的学员
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录
     * @param session http
     * @return
     */
    @RequestMapping(value = "/order/surplus/class/num/list", method = RequestMethod.POST)
    @ResponseBody
    public String getSurplusClassLessNUmList(Integer ccId, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchSurplusClassLessNUmList(ccId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页未设置动态提醒学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 老学员跟进
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录数
     * @param session http
     * @return
     */
    @RequestMapping(value = "/oldStuFollow", method = RequestMethod.POST)
    @ResponseBody
    public String oldStuFollow(Integer ccId,Integer followDays, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchOcOldStudent(ccId,followDays, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页cc老学员跟进列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 自动约课失败记录
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录数
     * @param session 会话对象
     * @return
     */
    @RequestMapping(value = "/auto/booked/fail/list", method = RequestMethod.POST)
    @ResponseBody
    public String getAutoBookedFailList(Integer ccId, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchAutoBookedFailList(ccId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页cc自动约课失败列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 学员上课表现三星以下记录
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录数
     * @param session 会话对象
     * @return
     */
    @RequestMapping(value = "/class/attitude/list", method = RequestMethod.POST)
    @ResponseBody
    public String getClassAttitudeList(Integer ccId, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchClassAttitudeList(ccId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页cc学员上课表现三星以下记录列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 学员上周、本周、下周课程数少于3节记录
     * @param ccId cc id
     * @param weekId 上周(-1),本周(0),下周(1)
     * @param page 页码
     * @param limit 页记录数
     * @param session 会话对象
     * @return
     */
    @RequestMapping(value = "/class/week/info/list", method = RequestMethod.POST)
    @ResponseBody
    public String getWeekClassInfoList(Integer ccId,Integer weekId, Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchWeekClassInfoList(ccId,weekId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页cc学员上周、本周、下周课程数少于3节记录列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * cc名下学员当天课程
     * @param ccId cc id
     * @param classState 课程状态
     * @param page 页码
     * @param limit 页记录数
     * @param session 会话对象
     * @return
     */
    @RequestMapping(value = "/class/today/booked/list", method = RequestMethod.POST)
    @ResponseBody
    public String getBookedCourseTodayList(Integer ccId,Integer classState,Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        if (classState==null){
            classState=1;
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchBookedCourseTodayList(ccId,classState, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页cc名下学员当天课程记录列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * cc名下学员最近两次成绩
     * @param ccId cc id
     * @param searchKey 查询关键字
     * @param page 页码
     * @param limit 页记录数
     * @param session 会话对象
     * @return
     */
    @RequestMapping(value = "/last/two/score/list", method = RequestMethod.POST)
    @ResponseBody
    public String getLastTwoScoreList(Integer ccId,String searchKey,Integer page, Integer limit, HttpSession session) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if (ccId == null) {
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchLastTwoScoreList(ccId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页cc名下学员最近两次成绩记录列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }
}
